package com.tian.service.impl;


import com.alibaba.fastjson.JSON;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.tian.common.CommonResult;
import com.tian.dto.ChargingCountRespDto;
import com.tian.dto.ChargingRecordPageReqDto;
import com.tian.dto.ChargingRecordPageRespDto;
import com.tian.dto.ChargingRecordRespDto;
import com.tian.entity.ChargeRecord;
import com.tian.entity.ChargeStationGun;
import com.tian.mapper.ChargeRecordMapper;
import com.tian.service.ChargingRecordManagementService;
import com.tian.util.DateUtils;
import com.tian.util.StationRedisPreKey;
import com.tian.util.StringUtil;
import lombok.extern.slf4j.Slf4j;
import org.redisson.api.RBucket;
import org.redisson.api.RedissonClient;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.List;

/**
 * {@code @description:} 充电服务实现类
 *
 * @author tianwc 公众号：Java后端技术全栈
 * 在线刷题 1200+java面试题和1000+篇技术文章：<a href="https://woaijava.cc/">博客地址</a>
 * {@code @date:} 2024-08-29 18:30
 * {@code @version:} 1.0
 */
@Slf4j
@Service
public class ChargingRecordManagementServiceImpl implements ChargingRecordManagementService {

    @Resource
    private ChargeRecordMapper chargeRecordMapper;
    @Resource
    private RedissonClient redissonClient;

    @Override
    public CommonResult<ChargingCountRespDto> count() {

        long count = chargeRecordMapper.countTimes();
        BigDecimal sumAmount = chargeRecordMapper.sumAmount();
        ChargingCountRespDto chargingCountRespDto = new ChargingCountRespDto();
        chargingCountRespDto.setChargingTimesCount(count);
        chargingCountRespDto.setChargingAmountCount(sumAmount.setScale(2, RoundingMode.HALF_UP)+"元");
        // TODO 其他统计
        chargingCountRespDto.setChargingTimesCountLastMonth(20);
        chargingCountRespDto.setChargingAmountCountLastMonth(0);
        chargingCountRespDto.setChargingExceptionCount(0);
        chargingCountRespDto.setChargingExceptionCountLastMonth(0);
        return CommonResult.success(chargingCountRespDto);
    }

    @Override
    public CommonResult<ChargingRecordPageRespDto> list(ChargingRecordPageReqDto chargingRecordPageReqDto) {
        String gunNo = chargingRecordPageReqDto.getGunNo();
        if (!StringUtil.isEmpty(gunNo)) {
            RBucket<String> chargeStationGunBucketByNo = redissonClient.getBucket(StationRedisPreKey.STATION_GUN_NO_PRE_KEY + gunNo);
            ChargeStationGun chargeStationGun = JSON.parseObject(chargeStationGunBucketByNo.get(), ChargeStationGun.class);
            if (chargeStationGun != null) {
                chargingRecordPageReqDto.setStationGunId(chargeStationGun.getId());
            }
        }
        Page<ChargeRecord> chargeRecordPage = PageHelper.startPage(chargingRecordPageReqDto.getCurrentPage(), chargingRecordPageReqDto.getPageSize());
        List<ChargeRecord> chargeRecords = chargeRecordMapper.selectByCondition(chargingRecordPageReqDto);
        ChargingRecordPageRespDto chargingRecordPageRespDto = new ChargingRecordPageRespDto();
        List<ChargingRecordRespDto> data = new ArrayList<>();
        chargeRecords.forEach(chargeRecord -> {
            ChargingRecordRespDto chargingRecordRespDto = new ChargingRecordRespDto();
            chargingRecordRespDto.setId(chargeRecord.getId());
            if(!StringUtil.isEmpty(gunNo)){
                chargingRecordRespDto.setGunNo(gunNo);
            }else {
                RBucket<String> chargeStationGunBucket = redissonClient.getBucket(StationRedisPreKey.STATION_GUN_PRE_KEY + chargeRecord.getStationGunId());
                ChargeStationGun chargeStationGun = JSON.parseObject(chargeStationGunBucket.get(), ChargeStationGun.class);
                chargingRecordRespDto.setGunNo(chargeStationGun.getGunNo());
            }

            chargingRecordRespDto.setStartTime(DateUtils.format(chargeRecord.getStartTime(), DateUtils.FORMAT_SECOND));
            chargingRecordRespDto.setEndTime(DateUtils.format(chargeRecord.getEndTime(), DateUtils.FORMAT_SECOND));
            chargingRecordRespDto.setQuantity(chargeRecord.getQuantity());
            chargingRecordRespDto.setPrice(chargeRecord.getPrice());
            chargingRecordRespDto.setChargeStatus(chargeRecord.getChargeStatus());
            chargingRecordRespDto.setPayStatus(chargeRecord.getPayStatus());
            BigDecimal amount = chargeRecord.getAmount().setScale(2, RoundingMode.HALF_UP);
            chargingRecordRespDto.setAmount(amount+"元");
            chargingRecordRespDto.setPayTime(chargeRecord.getPayTime() == null ? "" : DateUtils.format(chargeRecord.getPayTime(), DateUtils.FORMAT_SECOND));

            data.add(chargingRecordRespDto);
        });

        chargingRecordPageRespDto.setTotal((int) chargeRecordPage.getTotal());
        chargingRecordPageRespDto.setPageSize(chargingRecordPageReqDto.getPageSize());
        chargingRecordPageRespDto.setCurrentPage(chargingRecordPageReqDto.getCurrentPage());
        chargingRecordPageRespDto.setData(data);
        return CommonResult.success(chargingRecordPageRespDto);
    }
}
